var express = require('express');
var router = express.Router();
const {formatResponse , analysisToken} = require('../utils/tool')

const {loginService} = require('../service/adminServices')

// 登录
router.post('/login', async function (req, res, next) {
    console.log('req boday' , req.body)
    // 1.验证码验证

    // 2.保存
    const result = await loginService(req.body)
    if (result.token) {
        res.setHeader('authentication':result.token)
    }
    res.send(formatResponse(0 , '' , result.data))
});

// 恢复登录状态
router.get('/whoami' , async function (req, res, next){
    const token = analysisToken(req.get('Authorization'))
    res.send(formatResponse(0 , '' , {loginId: token.loginId , name:token.name , id:token.id }))
    console.log(token , 'token')
})

module.exports = router;
